package com.luo.jiuyuetest.controller;

import com.luo.jiuyuetest.dto.SupplierAddRequest;
import com.luo.jiuyuetest.dto.SupplierTreeDTO;
import com.luo.jiuyuetest.service.impl.SupplierServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 供应商控制器
 */
@RestController
@RequestMapping("/api/supplier")
public class SupplierController {

    @Autowired
    private SupplierServiceImpl supplierService;

    /**
     * 添加供应商及其产品和零件
     * 接口：POST /api/supplier/add
     * @param request 请求参数（JSON）
     * @return 成功消息
     */
    @PostMapping("/add")
    public String addSupplier(@RequestBody SupplierAddRequest request) {
        try {
            String result = supplierService.addSupplierWithProducts(request);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return "添加失败：" + e.getMessage();
        }
    }

    /**
     * 获取供应商-产品-零件的树形结构
     * 接口：POST /api/supplier/getTreeStructure
     * @return 树形结构数据
     */
    @PostMapping("/getTreeStructure")
    public List<SupplierTreeDTO> getTreeStructure() {
        try {
            List<SupplierTreeDTO> result = supplierService.getTreeStructure();
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}